package com.kzd.btgame.main.mine

import android.content.Intent
import android.view.LayoutInflater
import androidx.lifecycle.LifecycleOwner
import com.kzd.btgame.base.BaseViewModel
import com.kzd.btgame.custom.setOnClickAntiShakeListener
import com.kzd.btgame.databinding.ActivityMineInviteBinding
import com.kzd.btgame.entity.UserInvite
import com.module.lemlin.app.OwnerStatusBar
import com.module.lemlin.http.Status
import com.module.lemlin.owner.OwnerViewBindingActivity
import com.module.lemlin.owner.ownerViewModel
import com.umeng.socialize.UMShareAPI

class MineInviteActivity : OwnerViewBindingActivity<ActivityMineInviteBinding>() {

    private val mViewModel: BaseViewModel by lazy {
        ownerViewModel(owner = this)
    }

    override val inflate: (LayoutInflater) -> ActivityMineInviteBinding
        get() = ActivityMineInviteBinding::inflate

    override fun initStatusBar(statusBar: OwnerStatusBar) {
        statusBar.run {
            addView = mViewBinding.flMineInviteBar
        }
        super.initStatusBar(statusBar)
    }

    override fun initView() {

        mViewBinding.tvMineInviteSubmit.setOnClickAntiShakeListener {
            val userInvite = mViewModel.userInviteLiveData.value
                ?.data?.data ?: return@setOnClickAntiShakeListener
            mViewModel.userShare(
                activity = this,
                url = userInvite.friend_url,
                title = userInvite.share_title,
                desc = userInvite.share_content
            )
        }
    }

    override fun initData() {
        mViewModel.userInvite()
    }

    override fun initViewMode() {
        mViewModel.userInviteLiveData.observe(this as LifecycleOwner, { response ->
            if (response.status != Status.SUCCESS) {
                return@observe
            }
            val data = response.data?.data
                ?: return@observe
            fillData(data)
        })
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data)
    }

    private fun fillData(userInvite: UserInvite) {
        mViewBinding.tvMineInviteCountPeople.text = userInvite.num.toString()
        mViewBinding.tvMineInviteCountIntegral.text = userInvite.points
    }
}